//
//  ZHProgressHUD.m
//  ProfilePicture
//
//  Created by 张云江 on 2019/10/21.
//  Copyright © 2019年 张云江. All rights reserved.
//

#import "ZHProgressHUD.h"

@implementation ZHProgressHUD

+ (ZHProgressHUD *)showMessage:(NSString *)message
{
    ZHProgressHUD *hud = [[ZHProgressHUD alloc] initWithFrame:WCDelegate.window.frame];
    [hud setMessageText:message];
    return hud;
    
}

+ (void)hideHUD
{
    NSEnumerator *subviewsEnum = [WCDelegate.window.subviews reverseObjectEnumerator];
    for (UIView *subview in subviewsEnum) {
        if ([subview isKindOfClass:self]) {
            [subview removeFromSuperview];
        }
    }
}

- (void)hideHUD
{
    [self removeFromSuperview];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI
{
    self.backgroundColor = kUIColorFromRGA(0x000000, .5);
    [WCDelegate.window addSubview:self];
    
    UIView *hud = [[UIView alloc]init];
    hud.backgroundColor = [UIColor whiteColor];
    hud.layer.cornerRadius = kRatio(20);
    hud.layer.masksToBounds = YES;
    [self addSubview:hud];
    [hud mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.equalTo(self);
        make.width.mas_equalTo(kRatio(156));
        make.height.mas_equalTo(kRatio(172));
    }];
    
    self.launchAnimation = [LOTAnimationView animationNamed:@"loading_figure"];
    self.launchAnimation.cacheEnable = NO;
    
    self.launchAnimation.frame = CGRectMake((kScreenWidth - kRatio(120))/2, (kScreenHeight - kRatio(120 - 30))/2, kRatio(120), kRatio(120));
    self.launchAnimation.contentMode = UIViewContentModeScaleToFill;
    self.launchAnimation.animationSpeed = 1;
    self.launchAnimation.loopAnimation = YES;
    
    [self addSubview:self.launchAnimation];
    [self.launchAnimation play];
    
    self.labelText = [[UILabel alloc]init];
    self.labelText.font = [UIFont systemFontOfSize:kRatio(14)];
    self.labelText.textColor = kUIColorFromRGA(0x000000, .65);
    self.labelText.textAlignment = NSTextAlignmentCenter;
    [hud addSubview:self.labelText];
    [self.labelText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(hud.mas_left);
        make.right.equalTo(hud.mas_right);
        make.height.mas_equalTo(kRatio(14));
        make.bottom.equalTo(hud.mas_bottom).offset(-kRatio(20));
    }];
}

- (void)setMessageText:(NSString *)messageText
{
    self.labelText.text = messageText;
}
@end
